import { defineStore } from 'pinia';
import { PageVO } from '@/entiy/vo/PageVO.ts';
import { XxlJobVO } from '@/entiy/vo/XxlJobVO.ts';
import XEUtils from 'xe-utils';
import RequestService from '@/service/RequestService.ts';
import { XxlJobListFormData } from '@/entiy/form/XxlJobListFormData.ts';
import PromiseUtils from '@/utils/PromiseUtils.ts';

export const useXxlJobListStore = defineStore('useXxlJobListStore', {
    state: () => {
        return {
            // 当前页码
            currentPage: 1,

            // 每页数据量
            currentPageSize: 10,

            // 总数据量
            totalRow: 0,

            // 列表
            dataList: [] as XxlJobVO[],
        }
    },

    getters: {},

    actions: {
        // 查询任务列表数据
        async getJobList(formData: XxlJobListFormData): Promise<void> {
            try {
                const data = {pageNum: this.currentPage, pageSize: this.currentPageSize, startDate: formData.startDate, endDate: formData.endDate}
                const result = await RequestService.post<PageVO<XxlJobVO>>('/get-xxl-error-job-list', data);
                if (result.isError) {
                    return Promise.reject(result.errorMsg);
                }

                // 总数据量
                this.totalRow = XEUtils.isNull(result.bizDta.totalRow) ? 0 : result.bizDta.totalRow;

                // 列表
                this.dataList = XEUtils.isNull(result.bizDta.recordList) ? [] : result.bizDta.recordList;
                return PromiseUtils.resolve();
            } catch (e) {
                console.log("GetJobList Error:", e);
                return Promise.reject("系统繁忙, 请稍后再试");
            }
        }
    },
});
